﻿#if UNITY_EDITOR

using System;
using FutureCore;
using FutureCorePlugin;
using Game.Fight;
using Spine.Unity;
using UnityEngine;

namespace DesignerProject
{
    public class SkillEditor : MonoBehaviour
    {
        public enum Mode
        {
            [EnumLabel("策划编辑")]
            ProcedureEdit,
            //[EnumLabel("正式查看")]
            //OfficialCheck,
        }
        public enum CampType
        {
            [EnumLabel("进攻兵种")]
            Atk,
            [EnumLabel("防守兵种")]
            Def,
        }
        public enum AnimType
        {
            [EnumLabel("普攻")]
            Atk,
            [EnumLabel("技能")]
            SKill,
        }

        [EnumLabel("模式")]
        public Mode currMode = Mode.ProcedureEdit;

        // Info
        [EnumLabel("攻守阵营")]
        public CampType campType = CampType.Atk;
        public int skillArmsId = 21001;
        [EnumLabel("动作")]
        public AnimType animType = AnimType.Atk;
        public int beSkillArmsId = 11001;

        // ProcedureEdit
        public string hitEffectId = "40000_sj";
        public float hitEffectScale = 1f;
        public bool isSkillBindBullet = true;

        public float bulletSpeed = 2f;
        public float bulletOffset = 0;
        public bool isCollimation = true;
        public string bulletEffectId = "40000_dd";
        public float bulletEffectScale = 1f;

        public string buffEffectId = "30001";
        public float buffEffectScale = 1f;

        private ObjModelVO modelVo;
        private ObjModelVO beModelVo;

        private GameObject skillActorGo;
        private GameObject beSkillActorGo;
        private SkeletonAnimation skillAnimCom;
        private SkeletonAnimation beSkillAnimCom;

        private GameObject buffEf;

        private bool isFighting;

        private void Awake()
        {
            ConfigMgr.Instance.ParseAllConfig();
            TimersMgr.Instance.Init();
        }

        private void OnDestroy()
        {
            ConfigMgr.Instance.Dispose();
        }

        private void Update()
        {
            if (buffEf)
            {
                if (buffEf.transform.localScale.x != buffEffectScale)
                {
                    buffEf.transform.localScale = VectorUtil.GetSameXY1(buffEffectScale);
                }
            }
        }

        public void CreateActor()
        {
            DebugUtil.Log("创建兵种");
            CreateActors();
        }

        public void PlaySkillEffect()
        {
            DebugUtil.Log("播放技能特效");
            EditPlaySkillEffect();
        }

        public void PlayBuffEffect()
        {
            DebugUtil.Log("播放Buff特效");
            EditPlayBuffEfect();
        }

        private void CreateActors()
        {
            if (skillActorGo != null && beSkillActorGo != null)
            {
                Destroy(skillActorGo);
                Destroy(beSkillActorGo);
            }

            int skillArmsModelId;
            if (campType == CampType.Atk)
            {
                skillArmsModelId = AtkArmsVOModel.Instance.GetVO(skillArmsId).modelId;
            }
            else
            {
                skillArmsModelId = DefArmsVOModel.Instance.GetVO(skillArmsId).modelId;
            }
            int beSkillArmsModelId;
            if (campType == CampType.Atk)
            {
                beSkillArmsModelId = DefArmsVOModel.Instance.GetVO(beSkillArmsId).modelId;
            }
            else
            {
                beSkillArmsModelId = AtkArmsVOModel.Instance.GetVO(beSkillArmsId).modelId;
            }

            skillActorGo = InsLoad("Skeleton/" + skillArmsModelId);
            skillActorGo.transform.position = new Vector3(4.95f, -13.67f, 0);
            modelVo = ObjModelVOModel.Instance.GetVO(skillArmsModelId);
            skillActorGo.transform.localScale = new Vector3(modelVo.scale, modelVo.scale, 0);
            skillAnimCom = skillActorGo.transform.GetChild(0).GetComponent<SkeletonAnimation>();
            SpineHelper.SetSortingOrder(skillAnimCom, 80);

            beSkillActorGo = InsLoad("Skeleton/" + beSkillArmsModelId);
            beSkillActorGo.transform.position = new Vector3(8.1f, -13.67f, 0);
            beModelVo = ObjModelVOModel.Instance.GetVO(beSkillArmsModelId);
            beSkillActorGo.transform.localScale = new Vector3(beModelVo.scale, beModelVo.scale, 0);
            beSkillAnimCom = beSkillActorGo.transform.GetChild(0).GetComponent<SkeletonAnimation>();
            SpineHelper.SetSortingOrder(beSkillAnimCom, 80);
            SpineHelper.SetFlipX(beSkillAnimCom, true);
        }

        private void EditPlaySkillEffect()
        {
            if (isFighting)
            {
                return;
            }
            isFighting = true;
            FightActorAnimType actorAnimType = FightActorAnimType.Atk;
            FightAnimFrameConfig frameCofing = modelVo.AtkFrameConfig;
            if (animType == AnimType.SKill)
            {
                actorAnimType = FightActorAnimType.Skill;
                frameCofing = modelVo.SkillFrameConfig;
            }
            TimersUtil.FightSimpleTimer.AddTimer(frameCofing.keyFrameTime, KeyFrameTask);
            TimersUtil.FightSimpleTimer.AddTimer(frameCofing.overFrameTime, OverFrameTask);
            SpineHelper.PlayAnim(skillAnimCom, actorAnimType.ToString());
            SpineHelper.AddLoopPlayAnim(skillAnimCom, FightActorAnimType.Idle.ToString());
        }

        public void PlayBulletEffect(Action func = null)
        {
            Transform selfAtkPoint = skillAnimCom.transform.GetChild(0).GetChild(1);
            Transform tarHitPoint = beSkillAnimCom.transform.GetChild(0).GetChild(0);
            GameObject bulletEf = InsLoad("Effect/" + bulletEffectId);
            bulletEf.transform.position = selfAtkPoint.position;
            bulletEf.transform.localScale = VectorUtil.GetSameXY1(bulletEffectScale);
            if (bulletOffset == 0)
            {
                LineBullet bullet = bulletEf.AddComponent<LineBullet>();
                bullet.onComplete = (goArg) =>
                {
                    if (func != null)
                    {
                        func();
                    }
                    Destroy(goArg);
                };
                bullet.Fire(tarHitPoint, bulletSpeed, isCollimation);
            }
            else
            {
                DirParabolaBullet bullet = bulletEf.AddComponent<DirParabolaBullet>();
                bullet.onComplete = (goArg) =>
                {
                    if (func != null)
                    {
                        func();
                    }
                    Destroy(goArg);
                };
                bullet.Fire(tarHitPoint, bulletSpeed, bulletOffset, isCollimation);
            }
        }

        private void KeyFrameTask()
        {
            if (isSkillBindBullet)
            {
                PlayBulletEffect(PalyHit);
            }
            else
            {
                PalyHit();
            }
        }

        private void PalyHit()
        {
            GameObject hitEffect = InsLoad("Effect/" + hitEffectId);
            hitEffect.transform.position = beSkillAnimCom.transform.GetChild(0).position;
            hitEffect.transform.localScale = VectorUtil.GetSameXY1(hitEffectScale);
            EffectTask task = hitEffect.GetComponent<EffectTask>();
            if (task != null)
            {
                task.taskFunc = Destroy;
            }
        }

        private void OverFrameTask()
        {
            isFighting = false;
        }

        private void EditPlayBuffEfect()
        {
            if (buffEf != null)
            {
                Destroy(buffEf);
            }
            buffEf = InsLoad("Effect/" + buffEffectId);
            buffEf.SetParent(skillActorGo);
            buffEf.transform.localScale = VectorUtil.GetSameXY1(buffEffectScale);
        }

        private GameObject InsLoad(string path)
        {
            return Instantiate(Resources.Load<GameObject>(path));
        }
    }
}

#endif